package com.osell.selection.ui.common;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.HomeActivity;
import com.da.lon.wang.xlg.activity.SettingInfoActivity;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.osell.selection.ui.base.BaseActivity;
import com.osell.selection.util.LocaleHelper;
import com.tencent.tauth.Tencent;
import com.umeng.analytics.MobclickAgent;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SettingActivity extends BaseActivity implements OnClickListener {

    @BindView(R.id.ouka_home_buyer_left)
    ImageButton left;
    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.fl_setting_user_info)
    FrameLayout fl_setting_user_info;
    @BindView(R.id.fl_setting_language)
    FrameLayout fl_setting_language;
    @BindView(R.id.tv_current_language)
    TextView tv_current_language;

    @BindView(R.id.login_out_wv)
    WebView login_out_wv;
    @BindView(R.id.setting_exit)
    Button setting_exit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);

        title.setText(R.string.settings);
        left.setOnClickListener(this);
        fl_setting_user_info.setOnClickListener(this);
        fl_setting_language.setOnClickListener(this);
        setting_exit.setOnClickListener(this);

        tv_current_language.setText(TextUtils.equals(Locale.ENGLISH.getLanguage(), LocaleHelper.getLanguage(this))
                ? getString(R.string.english) : getString(R.string.simplified_chinese));
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.fl_setting_user_info:
                intent = new Intent(this, SettingInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.fl_setting_language:
                new LanguageFragment().show(getSupportFragmentManager(), "LanguageFragment");
                break;
            case R.id.setting_exit:
                // 退出登录
                exitDialog(SettingActivity.this);
                break;
            case R.id.ouka_home_buyer_left:
                // 后退
                finish();
                break;
            default:
                break;
        }
    }

    private void exitDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("您确定退出吗");
        builder.setPositiveButton("否", (dialog, whichButton) -> {

        });
        builder.setNeutralButton("是", (dialog, whichButton) -> {
            login_out_wv.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
            });
            //网页版登录退出
            login_out_wv.loadUrl(XLGConstants.WEB_URL + "Member-Logout.html");
            sureExit();
        });
        builder.show();
    }

    protected void sureExit() {
        PreferenceUtils.getInstance().SetSettingString(
                PreferencesConfig.USER_ID, "");
        PreferenceUtils.getInstance().SetSettingString(
                PreferencesConfig.USER_KEY, "");
        Tencent tencent = Tencent.createInstance(XLGConstants.APP_ID, this);
        if (tencent != null) {
            tencent.logout(this);
            tencent.releaseResource();
        }

        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    }
}
